package AgentesClassificadores;

import jade.core.behaviours.CyclicBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.lang.acl.ACLMessage;

import java.io.File;
import java.io.IOException;

import org.ini4j.Ini;
import org.ini4j.InvalidFileFormatException;


@SuppressWarnings("serial")
public class AgenciaRating extends jade.core.Agent {

	private Ini ini;
	private String caminhoFich = "config/classificadores/";
	String nomeFich = "ratingFornecedores";

	protected void setup() 
	{ 
		try {
			ini = new Ini(new File(caminhoFich + nomeFich + ".ini"));

		} catch (InvalidFileFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			DFAgentDescription dfd = new DFAgentDescription();
			dfd.setName( getAID() );
			ServiceDescription sd  = new ServiceDescription();
			sd.setType( "AgenciaRating" );
			sd.setName( getLocalName() );
			dfd.addServices(sd);
			DFService.register(this, dfd );  
		}
		catch (FIPAException fe) { 
			fe.printStackTrace(); 
		}


		addBehaviour(new CyclicBehaviour(this) 
		{
			public void action() 
			{
				ACLMessage msg= receive();
				if (msg!=null) {
					sendRating(msg.getContent(),msg);
				}
				block();
			}
		});


	}

	public void sendRating(String nomeEmpresa, ACLMessage msg) {
		String rating = ini.get("ratings", nomeEmpresa);

		ACLMessage reply = msg.createReply();
		reply.setContent(rating);
		send(reply);

	}
}